package com.yunhui.controller;

import com.yunhui.entity.Merchant;
import com.yunhui.service.impl.MerchantServiceImpl;
import com.yunhui.tool.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import org.springframework.util.StringUtils;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.sound.midi.Soundbank;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  商家的前端控制层
 * </p>
 *
 * @author WGH
 * @since 2019-12-09
 */
@Controller
@RequestMapping("/merchant")
public class MerchantController {

    @Autowired
    private MerchantServiceImpl impl;

    @RequestMapping("/Login")
    public String login(){
        return "Login";
    }

    @ResponseBody
    @RequestMapping(value = {"/doregist"})
    public String addUser(@RequestParam("account") String account,
                          @RequestParam("phone") String phone,
                          @RequestParam("eamil") String eamil,
                          /*@RequestParam("createDate") Date createDate,*/
                          @RequestParam("status") Integer status,
                          @RequestParam("password") String password,
                          @RequestParam("password2") String password2) {

        Integer statu = 0;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String stringdate = formatter.format(new Date()); //获取系统当前时间

        System.err.println("stringdate : " + stringdate);

        Date date = null;
        try {
            date=formatter.parse(stringdate); //将Spring类型转成Date类型
        }catch (Exception e){
            e.printStackTrace();
        }

        System.err.println("date : " + date);

        if(StringUtils.isEmpty(account)){
            return "用户名不能为空";
        }

        if(StringUtils.isEmpty(phone)){
            return "手机号码不能为空";
        }

        if(StringUtils.isEmpty(eamil)){
            return "邮箱不能为空";
        }

        if(StringUtils.isEmpty(password)){
            return "密码不能为空";
        }
        if(StringUtils.isEmpty(password2)){
            return "确认密码不能为空";
        }

        if(!password.equals(password2)){
            return "两次密码不相同，注册失败！！";
        }else {
            int res = impl.insertMerchant(account,phone,eamil,date,statu,password);
            if(res == 0){
                //return "注册失败！";
                return "Login";
            }else {
                //return "注册成功！";
                return "index";
            }
        }

    }

    /**
     * 获取用户名与密码，用户登录
     * @return 登录成功页面
     * @param account
     * @param password
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = {"/dologin"})
    public String userLogin(@RequestParam("account") String account,
                            @RequestParam("password") String password,
                            HttpServletRequest request){

        if(StringUtils.isEmpty(account)){
            return "用户名不能为空";
        }
        if(StringUtils.isEmpty(password)){
            return "密码不能为空";
        }

        Merchant merchant = impl.userLogin(account,password);

        if(merchant != null){                                                  //登录成功
            request.getSession().setAttribute("session_user",merchant);     //将用户信息放入session  用于后续的拦截器
            //return "登录成功";
            return "index";
        }
        //return "登录失败，用户名或密码错误";
        return "Login";
    }

    /**
     * 注册
     * @param merchant 参数封装
     * @return Result
     */
    @PostMapping(value = "/doregist2")
    public String regist(Merchant merchant){
        impl.regist(merchant);
        System.err.println(merchant);
        return "/Login";
    }







}

